package edu.cs346.group5.ui.userpages;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import edu.cs346.group5.ui.MenuFactory;
import edu.cs346.group5.ui.ResultsTable;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class search_user extends JFrame {

	private JPanel contentPane;
	private JTextField first_name;
	private JTextField last_name;
	private JTextField email;

	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	public search_user() {
		setTitle("Search For User");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][grow]", "[][][][][]"));
		
		JLabel lblFirstName = new JLabel("First Name:");
		contentPane.add(lblFirstName, "cell 0 0,alignx trailing");
		
		first_name = new JTextField();
		contentPane.add(first_name, "cell 1 0,growx");
		first_name.setColumns(10);
		
		JLabel lblLastName = new JLabel("Last Name: ");
		contentPane.add(lblLastName, "cell 0 1,alignx trailing");
		
		last_name = new JTextField();
		contentPane.add(last_name, "cell 1 1,growx,aligny top");
		last_name.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email:");
		contentPane.add(lblEmail, "cell 0 2,alignx trailing");
		
		email = new JTextField();
		contentPane.add(email, "cell 1 2,growx");
		email.setColumns(10);
		
		setJMenuBar(MenuFactory.getMenu(this));
		
		JButton btnNewButton = new JButton("Search");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				SearchUser();
				

				
				
			}

			private void SearchUser() {
				String dbName = "jdbc:mysql://cs341.cs.uwosh.edu:4381/cs341g5";
				try
			      {
			          Class.forName("com.mysql.jdbc.Driver");  // register the driver
	
			          Connection conn = DriverManager.getConnection(dbName,"cs341g5","d632449");

			          String query = "";
			          if((email.getText().length() == 0) && (last_name.getText().length() == 0) && (first_name.getText().length() == 0))
			          {
			        	  query = "SELECT * FROM Users";
			          }
			          else if((email.getText().length() != 0) && (last_name.getText().length() == 0) && (first_name.getText().length() == 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where email = " + quote + email.getText() + quote;
			        	  System.out.println(query);
			          }
			          else if((email.getText().length() == 0) && (last_name.getText().length() != 0) && (first_name.getText().length() == 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where last_name = " + quote + last_name.getText() + quote;
			        	  System.out.println(query);
			          }
			          else if((email.getText().length() == 0) && (last_name.getText().length() == 0) && (first_name.getText().length() != 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where first_name = " + quote + first_name.getText() + quote;
			        	  System.out.println(query);
			          }
			          else if((email.getText().length() != 0) && (last_name.getText().length() != 0) && (first_name.getText().length() == 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where last_name = " + quote + last_name.getText() + quote + " and email = " + quote + email.getText() + quote;
			        	  System.out.println(query);
			          }
			          else if((email.getText().length() != 0) && (last_name.getText().length() == 0) && (first_name.getText().length() != 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where first_name = " + quote + first_name.getText() + quote + " and email = " + quote + email.getText() + quote;
			        	  System.out.println(query);
			          }
			          else if((email.getText().length() == 0) && (last_name.getText().length() != 0) && (first_name.getText().length() != 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where last_name = " + quote + last_name.getText() + quote + " and first_name = " + quote + first_name.getText() + quote;
			        	  System.out.println(query);
			          }
			          else if((email.getText().length() != 0) && (last_name.getText().length() != 0) && (first_name.getText().length() != 0))
			          {
			        	  String quote = "\"";
			        	  query = "SELECT * FROM Users where last_name = " + quote + last_name.getText() + quote + " and email = " + quote + email.getText() + quote + " and first_name = " + quote + first_name.getText() + quote;
			        	  System.out.println(query);
			          }

			          
			  
			          java.sql.Statement st = conn.createStatement();
			          ResultSet rs = st.executeQuery(query);
			          ArrayList<String[]> rst = new ArrayList<String[]>();
			          String[] set = new String[3];
			          set[0] = "First Name";
			          set[1] = "Last Name";
			          set[2] = "Email";
			          rst.add(set);

			          while (rs.next()) {
			            String lastName = rs.getString("last_name");
			            String firstName = rs.getString("first_name");
			            String email = rs.getString("email");
			            set = new String[3];
			            set[0] = firstName;
			            set[1] = lastName;
			            set[2] = email;
			            rst.add(set);
			          }   
			          conn.close();
			          ResultsTable.main(rst, false);
			      }
			      catch (SQLException e1) {System.out.println("SQL Error " + e1.getMessage());}
			      catch (Exception e1) {System.out.println("General Error " + e1.getMessage());}
			}
		});
		contentPane.add(btnNewButton, "cell 1 4,alignx right");
	}

}


